<?php

namespace app\Modules\Admin\controllers;

use Yii;
use app\models\Pic;
use app\models\Saleorder;
use app\models\SaleorderSearch;
use app\models\Push;
use yii\data\Pagination;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;


/**
 * Class SaleorderController
 * @package app\Modules\Admin\controllers
 */
class SaleorderController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    /**
     * Lists all Product models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SaleorderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->setPagination(new Pagination(['pageSize'=>10]));
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Product model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $picDataProvider = new ActiveDataProvider([
            'query' => Pic::find()->where(['product_id'=>$model->product_id]),
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
        return $this->render('view', [
            'model' => $model,
            'picDataProvider'=>$picDataProvider,
        ]);
    }

    /**
     * 发货
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDeliver($id)
    {
        $order = $this->findModel($id);
        if($order->canChange(Saleorder::STEAT_SHIPPED)){
            $order->state = Saleorder::STEAT_SHIPPED;
            $order->save(false);
            Push::create(
                '卖家已发货',
                "卖家已经发货了{$order->pname}{$order->pspec},请耐心等待,保持电话畅通",
                $order->user_id
            );
        }

        return $this->redirect(['index']);
    }
    /**
     * Deletes an existing Product model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Product model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Saleorder the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Saleorder::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}